import {
  swiperItems_student,
  swiperItems_teacher
} from "./swiperItems";
var app = getApp();
Page({
  data: {
    swiperItems: [],
    defaultUrl: "/pages/sign/sign",
    isStudent: true, //真正是学生
    chooseStudent: true, //选择学生单选框
    sId: null,
    sImg: "/images/head.png",
    sName: "520快乐",
    sTodayCourse: null,
    showDot: 0,
    newMessageCount: app.globalData.newMessageCount,
  },

  onLoad: function (options) {
    var that = this;
    this.setData({
      swiperItems: swiperItems_student
    })
    getTodoList(that);
  },
  onShow() {
    var that = this;
    that.clear();
    var that = this;
    var _socket = app.globalData.socket
    console.log("_socket:" + _socket)
    if (_socket) {
      _socket.on("getHomework", (d) => {
        console.log(d)
        app.globalData.newMessageCount++;
        app.globalData.myhomework = true;
        that.setData({
          newMessageCount: app.globalData.newMessageCount,
        })

      })
      _socket.on("getMessageFoemSomeone", (d) => {
        console.log(d)
        app.globalData.newMessageCount++;

        that.setData({
          newMessageCount: app.globalData.newMessageCount,
        })
      })
      _socket.on("getNotice", (d) => {
        console.log(d)
        app.globalData.newMessageCount++;
        app.globalData.classgonggao = true;
        that.setData({
          newMessageCount: app.globalData.newMessageCount,
        })
      })
      _socket.on("setSignPeriod", (d) => {
        console.log("监听啦");
        d = JSON.parse(d)
        app.goToSign(d)
      })
    }
    // app.globalData.newMessageCount = 1;
    getTodoList(that);
    synchronizeDataFromGlobalData(that);
    setFunctionList(that);
    getUserInfo(that);
    console.log(this.data.sId)

    that.setData({
      newMessageCount: app.globalData.newMessageCount,
    })
  },

  clear() {
    app.globalData.newMessageCount = null
  },
  clear_change() {
    app.globalData.newMessageCount = null
    wx.showToast({
      title: '请等待老师点到',
      icon: 'success',
      duration: 1000
    })
  },
  radioChange_stuOrTea(e) {
    if (e.detail.value == 'teacher') {
      this.setData({
        swiperItems: swiperItems_teacher,
        chooseStudent: false,
      })
    } else {
      this.setData({
        swiperItems: swiperItems_student,
        chooseStudent: true,
      })
    }
    console.log("是学生吗" + this.data.chooseStudent);
    app.globalData.isStudent = this.data.chooseStudent
    console.log("是全局学生吗" + app.globalData.isStudent);
  },
  swiperChange(e) {
    //e.detail.current   0 1 2
    console.log(e)
    if (e.detail.current !== this.data.showDot) {
      this.setData({
        showDot: e.detail.current
      })
    }
  },
})
/**
 * 
 */
function setFunctionList(that) {
  if (app.globalData.isStudent) {
    that.setData({
      swiperItems: swiperItems_student,
    })
  } else {
    that.setData({
      swiperItems: swiperItems_teacher,
    })
  }
}
/**
 * 
 */
function getUserInfo(that) {
  if (app.globalData.sId) {
    if (app.globalData.isStudent) {
      wx.request({
        url: 'https://geek-team.xin/student/findById',
        data: {
          sId: that.data.sId,
        },
        success(e) {
          console.log(e)
          that.setData(e.data)
          app.globalData.sName = e.data.sName
          getTodayCourse(that)
        },
      })
    } else {
      wx.request({
        url: 'https://geek-team.xin/teacher/findById',
        data: {
          tId: that.data.sId,
        },
        success(e) {
          console.log(e)
          getTodayCourse(that);
        },
      })
    }
  }
}

/**
 * 同步全局变量到页面
 */
function synchronizeDataFromGlobalData(that) {
  that.setData({
    isStudent: app.globalData.isStudent,
    sId: app.globalData.sId,
    sImg: app.globalData.sImg,
    sName: app.globalData.sName,
    chooseStudent: app.globalData.isStudent
  })
}
/**
 * 获取备忘录
 */
function getTodoList(that) {
  wx.getStorage({
    key: 'todolist',
    success: function (res) {
      if (res.data) {
        that.setData({
          lists: res.data
        })
      }
    }
  })
}
/**
 * 获得今天课程
 */
function getTodayCourse(that) {
  console.log(that)
  var sb = that.data.isStudent ? 'Now' : 'ByDay';
  var wx_getWhat = that.data.isStudent ? '' : "Teacher";
  var idWhat = that.data.isStudent ? 'sId' : 'tId'
  wx.request({
    url: `https://geek-team.xin/schedule/wx_get${wx_getWhat}Schedule${sb}?${idWhat}=${that.data.sId}`,
    method: 'POST',
    success(res) {
      if (res.data != []) {
        that.setData({
          sTodayCourse: res.data
        })
        console.log(res.data)
      }
    },
    fail: (e) => console.log(e)
  })
}